﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Xml.Linq;

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            string strId = "10000";
            //string strXml = "ooooooooooooooooooooooooooooooooooooooooooooooooooooooooo";
            string strXml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                               "<Restaurant>" +
                               "<ID>10000</ID>" +
                               "<Name>aaa</Name>" +
                               "<Address>Minneapolis</Address>" +
                               "<OccupationRate>95</OccupationRate>" +
                               "<Latitude>44</Latitude>" +
                               "<Longitude>44</Longitude>" +
                               "</Restaurant>";
            /*XElement restaurant = new XElement("Restaurant");
            restaurant.Add(new XElement("ID", "10000"));
            restaurant.Add(new XElement("Address", "Minneapolis"));
            restaurant.Add(new XElement("RestaurantName", "aaa"));
            restaurant.Add(new XElement("OccupationRate", "95"));
            restaurant.Add(new XElement("Latitude", "44"));
            restaurant.Add(new XElement("Longitude","44"));*/

            //ASCIIEncoding encoding = new ASCIIEncoding();
            //UTF8Encoding encoding = new UTF8Encoding();
            //string postData = "action=search&location=0,0&distance=1000";
            //string postData = "action=delete&ID=10000";
            //string postData = strXml;
            //Encoding encoding = Encoding.GetEncoding("gb2312");
            string postData = "action=update&ID=10000";
            postData += ("&restaurant=" + strXml);
            Console.WriteLine(postData);
            byte[] data = Encoding.UTF8.GetBytes(postData);

            // Prepare web request...
            HttpWebRequest myRequest =
              (HttpWebRequest)WebRequest.Create("http://csci5221group05test.cloudapp.net/service.aspx");
            myRequest.Method = "POST";
            myRequest.ContentType = "application/x-www-form-urlencoded";
            myRequest.ContentLength = data.Length;
            
            Stream newStream = myRequest.GetRequestStream();

           
            // Send the data.
            newStream.Write(data, 0, data.Length);
            newStream.Close();
            try
            {
                WebResponse myResponse = myRequest.GetResponse();
                StreamReader sr = new StreamReader(myResponse.GetResponseStream());
                string result = sr.ReadToEnd();
                Console.WriteLine(result);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.ToString());
            }

            Console.ReadLine();

        }
    }
}
